package com.njtoyo.taxi.admin.rest.controller.business;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.master.entity.SimpleCall;
import com.njtoyo.taxi.admin.mapper.master.enums.SimpleCallRideCategory;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.SimpleRideDetailPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.simple_call.ListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.simple_call_ride.SimpleCallRideVO;
import com.njtoyo.taxi.admin.rest.wrapper.business.simpleCallRide.SimpleCallRideQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.simple_call.SimpleCallQueryWrapper;
import com.njtoyo.taxi.admin.service.business.BusinessSimpleCallService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@Api(value = "电召呼叫", tags = "电召呼叫")
@RequestMapping("business/simple_call")
public class BusinessSimpleCallController {

    @Autowired
    private BusinessSimpleCallService businessSimpleCallService;

    @GetMapping()
    @ApiOperation(value = "呼叫列表", notes = "权限：simple_call_list")
    @PermissionScope(names = {"simple_call_list"})
    RestResult<IPage<ListPresenter>> getList(@ModelAttribute("admin") AdminUser adminUser,SimpleCallQueryWrapper wrapper) {
        return businessSimpleCallService.getList(adminUser,wrapper);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "行程详情")
    RestResult<SimpleCall> getDetail(@PathVariable("id") Long id) {
        return businessSimpleCallService.detail(id);
    }

    @GetMapping("/{id}/cancel")
    @ApiOperation(value = "取消电召需求单")
    RestResult<SimpleCall> cancel(@PathVariable("id") Long id) {
        return businessSimpleCallService.cancel(id);
    }

    /**
     * 乘客使用最多的几条地址
     *
     * @param phone
     * @return
     */
    @GetMapping("/{phone}/getHisoryOriginTop3ByPhone")
    RestResult getHisoryOriginTop3ByPhone(@PathVariable("phone") String phone) {
        return businessSimpleCallService.getHisoryOriginTop3ByPhone(phone);
    }

    /**
     * 登录人操作记录
     *
     * @return
     */
    @GetMapping("getLoginUserOperationCall")
    RestResult getLoginUserCall() {
        return businessSimpleCallService.getLoginUserCall();
    }


    /**
     * 重新呼叫
     *
     * @return
     */
    @GetMapping("/{id}/getReCallInfo")
    RestResult getReCallInfo(@PathVariable Long id) {
        return businessSimpleCallService.getReCallInfo(id);
    }


    /**
     * 重新呼叫
     *
     * @return
     */
    @PostMapping("/{id}/reCall")
    RestResult reCallById(@PathVariable Long id) {
        return businessSimpleCallService.reCallById(id);
    }


    @GetMapping("/export_simple_call")
    @ApiOperation(value = "导出")
    void export(@ModelAttribute("admin") AdminUser adminUser,SimpleCallQueryWrapper queryWrapper, HttpServletResponse response) throws IOException {
        queryWrapper.setSize(100000);
        RestResult<IPage<ListPresenter>> page = businessSimpleCallService.getList(adminUser,queryWrapper);
        IPage<ListPresenter> listPage = page.getData();
        List<Map<String, Object>> rows = new ArrayList<>();
        if (CollUtil.isNotEmpty(listPage.getRecords())) {

            for (ListPresenter presenter : listPage.getRecords()) {
                Map<String, Object> row = new HashMap<>();
                row.put("呼叫单号", presenter.getId());
                row.put("操作员", presenter.getAdminName());
                if (ObjectUtil.isNotNull(presenter.getCallMode())) {
                    row.put("叫车模式", presenter.getCallMode().getDesc());
                } else {
                    row.put("叫车模式", "");
                }
                if (ObjectUtil.isNotNull(presenter.getCategory())) {
                    row.put("叫车类型", presenter.getCategory().getDesc());
                } else {
                    row.put("叫车类型", "");
                }
                row.put("地区", presenter.getCantonName());
                row.put("乘客编号", presenter.getCustomerId());
                row.put("乘客名称", presenter.getCustomerName());
                row.put("乘客电话", presenter.getCustomerMobileNumber());
                row.put("司机编号", presenter.getDriverId());
                row.put("司机名称", presenter.getDriverName());
                row.put("司机电话", presenter.getDriverMobileNumber());
                row.put("车牌", presenter.getTaxiPlateNumber());
                row.put("公司", presenter.getCantonName());
                row.put("起点", presenter.getOriginName());
                row.put("终点", presenter.getDestinationName());
                row.put("下单时间", DateUtil.formatDateTime(presenter.getCreatedAt()));
                rows.add(row);
            }
        } else {
            Map<String, Object> row = new HashMap<>();
            row.put("呼叫单号", "");
            row.put("操作员", "");
            row.put("叫车模式", "");
            row.put("叫车类型", "");
            row.put("地区", "");
            row.put("乘客编号", "");
            row.put("乘客名称", "");
            row.put("乘客电话", "");
            row.put("司机编号", "");
            row.put("司机名称", "");
            row.put("司机电话", "");
            row.put("车牌", "");
            row.put("公司", "");
            row.put("起点", "");
            row.put("终点", "");
            row.put("下单时间", "");
            rows.add(row);
        }
        ExcelWriter writer = ExcelUtil.getWriter();
        StyleSet style = writer.getStyleSet();
        CellStyle cellStyle = style.getHeadCellStyle();
        cellStyle.setWrapText(true);
        writer.write(rows, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=ride_call.xls");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);


    }


}
